﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace amuudp.package
{
    public class Fragment
    {
        public static byte[] CreateFragmentBytes(int packageId, int size, int index, byte[] data)
        {
            byte[] result = new byte[24 + data.Length];
            Array.Copy(BitConverter.GetBytes(packageId), 0, result, 0, 4);
            Array.Copy(BitConverter.GetBytes(size), 0, result, 8, 4);
            Array.Copy(BitConverter.GetBytes(index), 0, result, 16, 4);
            Array.Copy(data, 0, result, 24, data.Length);
            return result;
        }

        public int PackageId { get; private set; }

        public int Size { get; private set; }

        public int Index { get; private set; }

        public byte[] Bytes { get; private set; }

        public Fragment(byte[] bytes)
        {
            this.Bytes = bytes;
            this.PackageId = UdpCommon.ByteCopyToInt(bytes, 0);
            this.Size = UdpCommon.ByteCopyToInt(bytes, 8);
            this.Index = UdpCommon.ByteCopyToInt(bytes, 16);
        }

        public void ReadData(byte[] targetBytes, int targetStartIndex)
        {
            Array.Copy(Bytes, 24, targetBytes, targetStartIndex, Bytes.Length - 24);
        }
    }
}
